//
//  FSIngredientsVC.m
//  FieldStory
//
//  Created by itte on 2017/6/21.
//  Copyright © 2017年 funmain. All rights reserved.
//

#import "FSIngredientsVC.h"
#import "FSFoodChildVC.h"
#import "FSShoppingCartData.h"
#import "FSFoodListChildVC.h"
#import "FSFoodDetailVC.h"

#define kBoxBlueColor         @"#ADCA94"
#define KJSColor              @"#FED046"

@interface FSIngredientsVC ()<UICollectionViewDelegate,FSFoodChildDelegate,FSFoodListChildDelegate>
@property (weak, nonatomic) IBOutlet UIView *boxContentView;
@property (weak, nonatomic) IBOutlet UIView *boxView;
@property (weak, nonatomic) IBOutlet UIView *fullBoxView;
@property (weak, nonatomic) IBOutlet UIView *moneyView;
@property (weak, nonatomic) IBOutlet UIView *pullView;
@property (weak, nonatomic) IBOutlet UIView *contentView;

// 11个盒子View
@property (weak, nonatomic) IBOutlet UIView *boxView01;
@property (weak, nonatomic) IBOutlet UIView *boxView02;
@property (weak, nonatomic) IBOutlet UIView *boxView03;
@property (weak, nonatomic) IBOutlet UIView *boxView04;
@property (weak, nonatomic) IBOutlet UIView *boxView05;
@property (weak, nonatomic) IBOutlet UIView *boxView06;
@property (weak, nonatomic) IBOutlet UIView *boxView07;
@property (weak, nonatomic) IBOutlet UIView *boxView08;
@property (weak, nonatomic) IBOutlet UIView *boxView09;
@property (weak, nonatomic) IBOutlet UIView *boxView10;
@property (weak, nonatomic) IBOutlet UIView *boxView11;
@property (weak, nonatomic) IBOutlet UIView *boxView12;

@property (weak, nonatomic) IBOutlet UILabel *lbTotleMoney;
@property (weak, nonatomic) IBOutlet UILabel *lbDes;
@property (strong, nonatomic) NSArray *boxViewArray;
@property (strong, nonatomic) FSFoodChildVC *foodChildVC;
@property (strong, nonatomic) FSFoodListChildVC *listChildVC;
@property (strong, nonatomic) FSFoodDetailVC *detailChildVC;
@property (assign, nonatomic) CGFloat sumMoney;
@end

@implementation FSIngredientsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.sumMoney = 0.0f;
    
    // 加入购物车的列表子控制器
    self.listChildVC = [[FSFoodListChildVC alloc] init];
    [self.contentView addSubview:self.listChildVC.view];
    [self.listChildVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_contentView);
    }];
    WeakSelfType blockSelf = self;
    [self.listChildVC.btnUp bk_whenTapped:^{
        [blockSelf hideList];
    }];
    UISwipeGestureRecognizer *recognizerUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(hideList)];
    [recognizerUp setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [self.listChildVC.btnUp addGestureRecognizer:recognizerUp];
    
    self.listChildVC.delegate = self;
    [self addChildViewController:self.listChildVC];
    // 所以商品的子控制器
    self.foodChildVC = [[FSFoodChildVC alloc] init];
    [self.contentView addSubview:self.foodChildVC.view];
    [self.foodChildVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_contentView);
    }];
    self.foodChildVC.cltContentView.delegate = self;
    self.foodChildVC.delegate = self;
    [self addChildViewController:self.foodChildVC];
    
    // 结算按钮点击事件
    self.moneyView.userInteractionEnabled = YES;
    [self.moneyView bk_whenTapped:^{
        [blockSelf gotoPrePay];
    }];
    
    self.boxViewArray = @[_boxView01,_boxView02,_boxView03,_boxView04,_boxView05,_boxView06,_boxView07,_boxView08,_boxView09,_boxView10,_boxView11];
    [self getShopData];
    // 下拉按钮点击事件
    [self.pullView bk_whenTapped:^{
        [blockSelf showList];
    }];
    UISwipeGestureRecognizer *recognizerDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(showList)];
    [recognizerDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [self.boxContentView addGestureRecognizer:recognizerDown];
    
    // 商品详情子控制器
    self.detailChildVC = [[FSFoodDetailVC alloc] init];
    [self.view addSubview:self.detailChildVC.view];
    [self.detailChildVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(blockSelf.view);
    }];
    [self.view sendSubviewToBack:self.detailChildVC.view];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

#pragma mark - 按钮点击事件
-(void)showList
{
    [self.listChildVC reloadData];
    [self.contentView sendSubviewToBack:_foodChildVC.view];
    [self.contentView bringSubviewToFront:_listChildVC.view];
    self.pullView.hidden = YES;
}

-(void)hideList
{
    [self.contentView sendSubviewToBack:_listChildVC.view];
    [self.contentView bringSubviewToFront:_foodChildVC.view];
    self.pullView.hidden = NO;
}

- (IBAction)btnCloseClick
{
    [[FSShoppingCartData sharedInstance].boxArray removeAllObjects];
    [[FSShoppingCartData sharedInstance].dataArray removeAllObjects];
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 结算
-(void)gotoPrePay
{
    [self pushViewController:@"FSPrePayVC" withParams:@{@"data":[FSShoppingCartData sharedInstance].boxArray}];
}

#pragma mark - FSFoodChildDelegate
-(void)addProductToBox
{
    NSInteger num = 0;
    for (FSShopListModel *shop in [FSShoppingCartData sharedInstance].boxArray) {
        num += shop.payNum;
    }
    if (num == 11) {
        [self.boxView setHidden:YES];
        [self.boxContentView bringSubviewToFront:self.fullBoxView];
        self.fullBoxView.backgroundColor = [UIColor colorwithHexString:kBoxBlueColor];
        self.moneyView.backgroundColor = [UIColor colorwithHexString:KJSColor];
        self.lbDes.text = @"立即结算 →";
        self.lbDes.textColor = [UIColor blackColor];
        self.lbDes.font = [UIFont systemFontOfSize:15];
        self.lbTotleMoney.text = [NSString stringWithFormat:@"￥%.2f",[self calculatePrice]];
        return ;
    }
    [self.boxView setHidden:NO];
    [self.boxContentView sendSubviewToBack:self.fullBoxView];
    for (int i=0; i<11; i++) {
        if (i<num) {
            UIView *boxView = self.boxViewArray[i];
            boxView.backgroundColor = [UIColor colorwithHexString:kBoxBlueColor];
        }
        else{
             UIView *boxView = self.boxViewArray[i];
            boxView.backgroundColor = [UIColor groupTableViewBackgroundColor];
            
        }
    }
    self.moneyView.backgroundColor = [UIColor whiteColor];
    NSString *str = [NSString stringWithFormat:@"还差%ld件满箱",(long)(11 - num)];
    self.lbDes.text = str;
    self.lbDes.textColor = [UIColor darkGrayColor];
    self.lbDes.font = [UIFont systemFontOfSize:13];
    [self changeLabel:self.lbDes withTextColor:[UIColor colorwithHexString:KJSColor]];
    self.lbTotleMoney.text = [NSString stringWithFormat:@"￥%.2f",[self calculatePrice]];
}

-(void)refreshBoxStatus
{
    [self addProductToBox];
}

-(CGFloat)calculatePrice
{
    self.sumMoney = 0.0;
    for (FSShopListModel *shop in [FSShoppingCartData sharedInstance].boxArray) {
        self.sumMoney += ([shop.vip_price.trimString floatValue]*shop.payNum);
    }
    return self.sumMoney;
}

- (void)changeLabel:(UILabel *)label withTextColor:(UIColor *)color {
    NSString *labelStr = label.text; //初始化string为传入label.text的值
    NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet]invertedSet];//创建一个字符串过滤参数,decimalDigitCharacterSet为过滤小数,过滤某个关键词,只需改变 decimalDigitCharacterSet类型  在将此方法增加一个 NSString参数即可
    NSInteger remainSeconde = [[labelStr stringByTrimmingCharactersInSet:nonDigits]intValue];//获取过滤出来的数值
    NSString *stringRange = [NSString stringWithFormat:@"%ld",(long)remainSeconde];//将过滤出来的Integer的值转换成String
    NSRange range = [labelStr rangeOfString:stringRange];//获取过滤出来的数值的位置
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:label.text];//创建一个带属性的string
    [attrStr addAttribute:NSForegroundColorAttributeName value:color range:range];//给带属性的string添加属性,attrubute:添加的属性类型（颜色\文字大小\字体等等）,value:改变成的属性参数,range:更改的位置
    label.attributedText = attrStr;//将 attstr 赋值给label带属性的文本框属性
}

#pragma mark - 网络请求数据
-(void)getShopData
{
    WeakSelfType blockSelf = self;
    [[AFNetworkManager sharedInstance] httpGetWithUrl:@"shop_goods_list" params:nil showHUD:NO toShowHUDView:nil success:^(id responseObject, NSString *errorMessage) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSArray *data = [FSShopListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [[FSShoppingCartData sharedInstance].dataArray removeAllObjects];
            [[FSShoppingCartData sharedInstance].dataArray addObjectsFromArray:data];
            [blockSelf.foodChildVC reloadData];
            [blockSelf.listChildVC reloadData];
        }
    } fail:^(NSString *errorCode, NSString *errorMessage) {
        
    }];
}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"-- %ld --",indexPath.row);
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}


@end
